package cate.game.role.pay.trademall.monthcard;

import cate.common.table.trademall.monthcard.MonthCardRow;
import cate.common.util.GameResult;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.pay.trademall.monthcard.msg.MonthCardInfoResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.HashMap;
import java.util.Map;

@NoteClass(value = "月卡数据")
public class MonthCardPO extends RoleDependent {
    @NoteField(value = "所有月卡档位数据")
    public Map<Integer, MonthCardStatusPO> monthCardMap;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (monthCardMap == null) {
            monthCardMap = new HashMap<>();
        }
        for (MonthCardRow configRow : role.getGame().table.tradeMall.month_card.getList()) {
            if (!monthCardMap.containsKey(configRow.id)) {
                MonthCardStatusPO po = new MonthCardStatusPO(configRow.id);
                monthCardMap.put(configRow.id, po);
            }
        }
        monthCardMap.values().forEach(po -> po.initialize(role));
    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        monthCardMap.values().forEach(po -> po.onDaySpan(silence));
        if (!silence) {
            noticeUpdate();
        }
    }

    /**
     * 同步信息
     */
    public void noticeUpdate() {
        role.sendNow(new MonthCardInfoResp(this));
    }

    /**
     * 领取月卡奖励
     *
     * @param tid 配置ID
     * @return 领取结果 无携带数据
     */
    @JsonIgnore
    public EcResult<Void> getMonthCardReward(int tid) {
        EcResult<Void> r = new EcResult<>();
        MonthCardStatusPO po = monthCardMap.get(tid);
        if (po == null) {
            return r.fail("月卡不存在");
        }
        return po.getReward(false);
    }

    /**
     * 检测是否允许激活月卡
     *
     * @param payItemId 充值项ID
     * @param tid       配置ID
     * @return 检测结果 无携带数据
     */
    public GameResult<Void> allowActiveMonthCard(int payItemId, int tid) {
        GameResult<Void> r = new GameResult<>();
        MonthCardStatusPO po = monthCardMap.get(tid);
        if (po == null) {
            return r.fail("月卡不存在");
        }
        return po.allowActive(payItemId);
    }

    /**
     * 激活月卡
     *
     * @param tid 配置ID
     */
    public void activeMonthCard(int tid) {
        MonthCardStatusPO po = monthCardMap.get(tid);
        if (po == null) {
            return;
        }
        po.active();
        noticeUpdate();
    }

    /**
     * 是否存在激活的至尊月卡
     */
    public boolean existActiveMonthCard() {
        for (MonthCardStatusPO statusPO : monthCardMap.values()) {
            if (statusPO.tid == 2 && statusPO.active) {
                return true;
            }
        }
        return false;
    }

    /**
     * 支付调用
     *
     * @param payNum 支付数量
     */
    public void onPay(long payNum) {
        monthCardMap.values().forEach(statusPO -> statusPO.onPay(payNum));
    }
}
